﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;

namespace Customize.Excel.Import
{
    [Serializable]
    public class ExcelMap : ICloneable
    {
        /// <summary>
        /// Excel表头的内容
        /// </summary>
        public string HeaderText { get; set; }

        /// <summary>
        /// 数据字段名
        /// </summary>
        public string FieldName { get; set; }
 

        /// <summary>
        /// 将Excel值转换为数据的特殊方法  
        /// </summary>
        public Func<string, object> Convert { get; set; }

        public ExcelMap()
        {
        }
        public ExcelMap(string excelText, string fieldName)
        {
            this.HeaderText = excelText;
            this.FieldName = fieldName;
        }
        public ExcelMap(string excelText, string fieldName, Func<string, object> cellConvert) 
            : this(excelText, fieldName)
        {
            this.Convert = cellConvert;
        }

        public object Clone()
        {
            var Formatter = new BinaryFormatter(null, new StreamingContext(StreamingContextStates.Clone));
            MemoryStream stream = new MemoryStream();
            Formatter.Serialize(stream, this);
            stream.Position = 0;
            object clonedObj = Formatter.Deserialize(stream);
            stream.Close();
            return clonedObj;
        }
    }
}
